
clear all 
set more off 
set maxvar 15000 
clear matrix


	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
    keep if baseline_sample==1

    gen decade2 = decade 
    replace decade2 =. if decade2>1940
    tab decade2, m

    levelsof decade, local(decades) 

    foreach variation in occ race south occ_race occ_south race_south occ_race_south  {

        forval i=1(1)2 {
            gen coeff_`i'_`variation'=. 
            gen est_lb_`i'_`variation' =.
            gen est_ub_`i'_`variation' =.
        }

        if "`variation'"=="occ" {
            local ige_dep "father_income_byocc"
            local rank_dep "rank_father_interp_occ"
        }
        if "`variation'"=="race" {
            local ige_dep "log_father_just_race_interp"
            local rank_dep "rank_father_justrace"
        }
        if "`variation'"=="south" {
            local ige_dep "log_father_just_south_interp"
            local rank_dep "rank_father_justsouth"
        }
        if "`variation'"=="occ_south" {
            local ige_dep "log_father_byoccsouth_interp"
            local rank_dep "rank_father_byoccsouth"
        }
        if "`variation'"=="occ_race" {
            local ige_dep "father_income_byrace"
            local rank_dep "rank_father_interp_byr"
        }    
        if "`variation'"=="race_south" {
            local ige_dep "log_father_just_rs_interp"
            local rank_dep "rank_father_just_rs"
        }
        if "`variation'"=="occ_race_south" {
            local ige_dep "log_father_baseline"
            local rank_dep "rank_father_baseline"
        }
    
    *1. IGE
        foreach x of local decades {
        quietly reg log_son_baseline `ige_dep'  if decade==`x' [pw=wgt_sex_race], robust 
            replace coeff_1_`variation' = _b[`ige_dep'] if decade==`x'
            replace est_ub_1_`variation' = _b[`ige_dep']+1.96*_se[`ige_dep'] if decade==`x' 
            replace est_lb_1_`variation' = _b[`ige_dep']-1.96*_se[`ige_dep'] if decade==`x' 
        }
            
    *2. Rank
        foreach x of local decades {
        quietly reg rank_son_baseline `rank_dep' if decade==`x' [pw=wgt_sex_race], robust 
            replace coeff_2_`variation' = _b[`rank_dep'] if decade==`x'
            replace est_ub_2_`variation' = _b[`rank_dep']+1.96*_se[`rank_dep'] if decade==`x'
            replace est_lb_2_`variation' = _b[`rank_dep']-1.96*_se[`rank_dep'] if decade==`x'
        }
        
    }
  
    bysort decade: keep if _n==1
    keep decade decade2 coeff_* est_*

*-------------------*
* Figures
*-------------------*

    * IGE (1910-1940 only, 7 variations)
    preserve 
        drop if decade>1940

        #delimit ;
        twoway  
                (connect coeff_1_occ decade, mcolor(purple) m(square) lc(purple) lpat(solid) lwidth(0.225))
                (connect coeff_1_occ_south decade, mcolor(black) m(diamond) lc(black) lpat(solid) lwidth(0.25))
                (connect coeff_1_occ_race decade, mcolor(lavender) m(square) lc(lavender) lpat(solid) lwidth(0.25))                
                (connect coeff_1_occ_race_south decade, mcolor(pink*1.25) m(triangle)  lc(pink*1.25) lpat(solid) lwidth(0.25))
                (connect coeff_1_race_south decade, mcolor(midblue*0.75) m(diamond_hollow) lc(midblue*0.75) lpat(solid) lwidth(0.25))
                (connect coeff_1_south decade, mcolor(navy*0.75) m(diamond_hollow) lc(navy*0.75) lpat(solid) lwidth(0.25))   
                (connect coeff_1_race decade,  mcolor(stone*1.25) m(triangle_hollow)  lc(stone*1.25) lpat(solid) lwidth(0.25))    
               ,
        xti(" " "Decade of respondent's birth") xlabel(1910(10)1940) xscale(range(1905 1945))
        legend(on row(3) size(small) symxsize(10) order(1 "Occ" 2 "Occ x south" 3 "Occ x race" 4 "Occ x race x south" 5 "Race x south" 6 "South only" 7 "Race only")) 
        ylabel(0.45(.15)1.05, axis(1)) yti("IGE coefficient" " ", axis(1)) 
        xlabel(1910 "1910s" 1920 "1920s" 1930 "1930s" 1940 "1940s", labsize(small) ) ;  
        #delimit cr
        graph export "$Mydirectory2/appendix_d/IGE_1910_1940_7variations.pdf", as(pdf) replace    
    restore
  
    * Rank (1910-1940 only, 7 variations, put race-only on different axis)
    preserve 
        drop if decade>1940

        #delimit ;
        twoway  
                (connect coeff_2_occ decade, mcolor(purple) m(square) lc(purple) lpat(solid) lwidth(0.225))
                (connect coeff_2_occ_south decade, mcolor(black) m(diamond) lc(black) lpat(solid) lwidth(0.25))
                (connect coeff_2_occ_race decade, mcolor(lavender) m(square) lc(lavender) lpat(solid) lwidth(0.25))                
                (connect coeff_2_occ_race_south decade, mcolor(pink*1.25) m(triangle)  lc(pink*1.25) lpat(solid) lwidth(0.25))
                (connect coeff_2_race_south decade, mcolor(midblue*0.75) m(diamond_hollow) lc(midblue*0.75) lpat(solid) lwidth(0.25))
                (connect coeff_2_south decade, mcolor(navy*0.75) m(diamond_hollow) lc(navy*0.75) lpat(solid) lwidth(0.25))   
                (connect coeff_2_race decade,  mcolor(stone*1.25) m(triangle_hollow)  lc(stone*1.25) lpat(solid) lwidth(0.25) yaxis(2))    
               ,
        xti(" " "Decade of respondent's birth") xlabel(1910(10)1940) xscale(range(1905 1945))
        legend(on row(3) size(small) symxsize(10) order(1 "Occ" 2 "Occ x south" 3 "Occ x race" 4 "Occ x race x south" 5 "Race x south" 6 "South only" 7 "Race only")) 
        ylabel(0.15(.1)0.55, axis(1)) yti("Rank coefficient" " ", axis(1)) yti(" " "Rank coefficient (race only)", axis(2))
        xlabel(1910 "1910s" 1920 "1920s" 1930 "1930s" 1940 "1940s", labsize(small) )  ;  
        #delimit cr
        graph export "$Mydirectory2/appendix_d/Rank_1910_1940_7variations_2axes.pdf", as(pdf) replace    
    restore 